﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using ListenMusic.User_ServiceReference;
using ListenMusic.Models.Reposistory.User;
using System.ServiceModel;
using System.IO;
namespace ListenMusic.Controllers.User
{
    public class UserController : ApplicationController
    {
        //
        // GET: /User/
       

        public UserController()
        {
           

        }
        public ActionResult Register()
        {
            create_Capcha();
            ViewBag.capcha = "../../Images/Capcha/" + Session["capcha"] + ".gif";
            return View("Register");
        }

        //public ActionResult Login(string email, string password, string returnURL)
        //{
        //    try
        //    {

        //        Member m = _user.checkLogin(email, password);
        //        if (m != null)
        //        {
        //            string[] array_user_info = new string[2];
        //            array_user_info[0] = m.Email;
        //            array_user_info[1] = m.MemberID.ToString();
        //            Session["user"] = array_user_info;
        //            //return RedirectToAction("Index", "Home");
        //            return Redirect(returnURL);
        //        }
        //        else
        //        {
        //            return Json(new { username = "fail", pass = "fail" });
        //        }

        //        //return RedirectToAction("Index","Home");
        //    }
        //    catch (FaultException ex)
        //    {
        //        return Redirect(returnURL);
        //        throw ex;
        //    }

        //}
        //public ActionResult Logout()
        //{
        //    try
        //    {
        //        if (Session["user"] != null)
        //            Session.Remove("user");
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new FaultException(ex.Message);
        //    }
        //    return RedirectToAction("Index", "Home");

        //}
        //public ActionResult ConfirmRegister(string email, string pwd, string name, string birthday, string phone, string capcha_input)
        //{
        //    _user = new UserReposistory();
        //    int maxID = _user.getMaxID_Member();
        //    string cap = Session["capcha"].ToString();
        //    if (capcha_input == cap)
        //    {
        //        Member member = new Member()
        //        {
        //            MemberID = maxID + 1,
        //            mPassword = pwd,
        //            Email = email,
        //            Phone = phone,
        //            MemberName = name,
        //            Roles = false,
        //            mStatus = true,
        //            Picture = "no-img.jpg",
        //            //Birthday = Convert.ToDateTime(birthday)
        //        };
        //        bool rs = _user.register(member);
        //        if (rs)
        //        {
        //            deleteAllCapcha();
        //            return RedirectToAction("Index", "Home");
        //        }
        //        else
        //            return RedirectToAction("Register", "User");
        //    }
        //    else
        //    {
        //        return RedirectToAction("Register", "User");
        //    }
        //}
        //[HttpPost]
        //public JsonResult checkEmail(string email)
        //{
        //    bool rs = _user.checkEmail(email);
        //    Response.ContentType = "application/json";
        //    return Json(rs, JsonRequestBehavior.AllowGet);
        //}
        public void create_Capcha()
        {
            Bitmap objBMP = new Bitmap(60, 20);
            Graphics objGraphics = Graphics.FromImage(objBMP);
            objGraphics.Clear(Color.Bisque);
            objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            Font objFont = new Font("Tahoma", 12, FontStyle.Bold);
            string randomStr = "";
            int[] myIntArray = new int[5];
            int x;
            Random autoRand = new Random();
            for (x = 0; x < 5; x++)
            {
                myIntArray[x] = Convert.ToInt32(autoRand.Next(0, 9));
                randomStr += (myIntArray[x].ToString());
            }
            Session.Add("capcha", randomStr);
            objGraphics.DrawString(randomStr, objFont, Brushes.White, 3, 3);
            //objBMP.Save(Response.OutputStream, ImageFormat.Gif);
            objBMP.Save(Server.MapPath("~/Images/Capcha/") + randomStr + ".gif", ImageFormat.Gif);
            //Response.ContentType = "image/GIF";
            objFont.Dispose();
            objGraphics.Dispose();
            objBMP.Dispose();
        }
        public void deleteAllCapcha()
        {
            string map = Server.MapPath("~/Images/Capcha/");
            string[] filePaths = Directory.GetFiles(map);

            foreach (string filepath in filePaths)
            {
                System.IO.File.Delete(filepath);
            }
        }
        public JsonResult updateLyrics(int songid, string lyrics)
        {
            SongReposistory _song = new SongReposistory();  
           
            bool rs = _song.updateLyrics(songid, lyrics);
            string mess;
            if (rs)
            {
                mess = "Thành công";
            }
            else
            {
                mess = "Thất bại";
            }
            return Json(mess, JsonRequestBehavior.AllowGet);
        }
    }
    
}
